/**
 * @Author:  renyc
 * @Date: 2023年3月3日13:10:09
 */

import * as Cesium from "cesium/Cesium";
import Layer from "../Layer";
import LayerType from "../LayerType";
import { SourceEventType } from "../../event/EventType";
import { LayerEventType } from "../../event/EventType";

/**
 * @class FeatureLayer
 * 
 *  1) source : 每一个FeatureLayer拥有一个FeatureSource作为Layer的数据源
 * Source : DataSource的派生类
 */
class FeatureLayer extends Layer{

    constructor(name, options){
        if(!options){
            throw new Error('FeatureLayer：options无效')
        }
        if(!options.source){
            throw new Error('FeatureLayer：source无效')
        }

        super(name, options);
        this._type = LayerType.FEATURE;

        //注册source的Event事件
        this._source.on(SourceEventType.FETCHED, this._onSourceFetched, this);

        //类型为：Cesium.GeoJsonDataSource
        this._delegate = undefined;
    }

    /**
     * 将Layer挂载到Viewer上
     * 由子类实现该函数
     * @protected
     * @Override 
     * @param {Viewer} viewer 
     */
    _attach(){
        if(this._viewer&&this._source){
            if(this._source._delegate){
                //source已经获取数据，直接将数据添加到Viewer上
                this._viewer.dataSources.add(this._source._delegate);
            }
            else{
                this._source.fetch();
            }
        }
    }

    /**
     * 将Layer从Viewer上卸载
     * 由子类实现该函数
     * @protected
     * @Override 
     */
    _detach(){
        // if(this._delegate){
        //     this._delegate.then(dataSource=>{
        //         dataSource.entities.removeAll();
        //     });
        // }

        if(this._source){
            this._source.cleanup();
            this._viewer.dataSources.add(this._source._delegate);
        }
    }

    /**
     * Source获取数据后的Event响应函数
     */
    _onSourceFetched(evt){
        let json = evt.json;
        let source = json.source;
        if(json){
            (new Cesium.GeoJsonDataSource()).load(json).then(dataSource=>{
                this._delegate = dataSource;
                this._viewer.dataSources.add(dataSource);

                this.render();

                this._fire(LayerEventType.LOADED, {obj:this});
            });
            // Cesium.GeoJsonDataSource(json).then(dataSource=>{
            //     this._delegate = dataSource;
            //     this._viewer.dataSources.add(dataSource);
            // });

        }
    }

    /**
     * @description 图层渲染函数
     * @override
     */
    render(){
        if(!this._delegate){
            return;
        }

        let dataSource = this._delegate;
        dataSource.entities.values.forEach(entity=>{
            // 重置图片，图片大小，大小单位为米
            //entity.billboard.image = imgPath || entity.properties.style.getValue().image;
            // entity.billboard.width = 30;
            // entity.billboard.height = 30;
            // entity.billboard.sizeInMeters = true;
            entity.label = {
                text : entity.name,
                font: '14pt Source Han Sans CN',
                
            }
        })
    }
}

export default FeatureLayer
